jQuery(document).ready(function() {

	var baseurlsummary = baseurl + "summary/";
	
	if (/summary/.test(self.location.href)){
	
		//var mobtel = getCookie("mobtel");
		var subsId = getCookie("subsId");
		//jQuery.getJSON(baseurlsummary+"retrieveUserProfileToDB/"+mobtel,{ajax:'true'},function(j){});
		jQuery.getJSON(baseurlsummary+"retrieveUserProfileToDB/"+subsId,{ajax:'true'},function(j){});
	}
	
	/* detect ctrl+v ,Ctrl+c */
	var ctrlDown = false;
    var ctrlKey = 17, vKey = 86, cKey = 67, xKey = 88
    $(document).keydown(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = true;
    }).keyup(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = false;
    });

	$('#mobile_number2,#verif_code2').keydown(function(e){
		if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey || e.keyCode == xKey)) return false;
    });
	//end

	jQuery('#agree').click(function(){
	   if(document.getElementById("agree").checked == true)
	      jQuery('#confirm').removeClass("disable");
	   else
		  jQuery('#confirm').addClass("disable");
	});
	
    jQuery('#confirm').click(function(){
    	if(jQuery(this).hasClass("disable")) {
		console.log("disabled");
    		return false;
    	} else {
    		// submit
    		//jQuery.getJSON(baseurlsummary+"sendPlanCode/"+mobtel,{ajax:'true'},function(j){
			subsId = getCookie("subsId");
			jQuery.getJSON(baseurlsummary+"sendPlanCode/"+subsId,{ajax:'true'},function(j){
    			if(j.status==0) {
    				jQuery("#nameReturned").text(j.name);
    				jQuery("#planCodemismo").text(j.planCode);
    				jQuery(".planCode").show();
					jQuery("#statusSummary").hide();
    				jQuery("#changedevice, #changepromodetails, #changemobtel, #changeuserprofile, #changestore, #termsandconditionscontainer, #footnote,  #termsandconditionscontainer").hide();
					jQuery("#printbutton").show();
					resizeIframe();
					scrollFrameHeight('0');
    			} else {
    				alert(j.description);
    			}
    		});
    	}
    });		
	
	jQuery('#changedevice').click(function(){
		document.cookie="mode=" + "edit" + "; path=/";
		window.location=baseurl + "gallery";
		document.cookie="current_pos=" + "5" + "; path=/";
		scrollFrameHeight('0');
		return false;
	});
	
	jQuery('#changepromodetails').click(function(){
		document.cookie="mode=" + "edit" + "; path=/";
		window.location=baseurl + "promo";
		document.cookie="current_pos=" + "5" + "; path=/";
		scrollFrameHeight('0');
		return false;
	});
	
	jQuery('#changemobtel').click(function(){
		document.cookie="mode=" + "edit" + "; path=/";
		jQuery('#mobile_number2').val("");
		jQuery('#verif_code2').val("");
		jQuery('#verificationCode2').hide();
		jQuery('.dialog_overlay').fadeIn();
		//jQuery('.modalStyle').css({"width":"575px","top":"555px"});
		jQuery('.modalStyle').css("width","575px");
		jQuery('#modalmobtel').fadeIn();
		jQuery('#modalmobtel').css({"z-index":"102","position":"fixed","top":"-150px","left":"28%"});
		//document.cookie="current_pos=" + "5" + "; path=/";
		jQuery('#notglobe').prop('checked', false);
		jQuery('#savechanges').addClass("disable");
		jQuery('#mobile_number2').focus();
		jQuery("#submit_mobtel2").text("Send code");
		scrollFrameHeight('0');
		return false;
	});
	
	jQuery('#changeuserprofile').click(function(){
		document.cookie="mode=" + "edit" + "; path=/";
		window.location=baseurl + "userprofile";
		document.cookie="current_pos=" + "5" + "; path=/";
		scrollFrameHeight('0');
		return false;
	});
	
	jQuery('#changestore').click(function(){
		document.cookie="mode=" + "edit" + "; path=/";
		window.location=baseurl + "globestore";
		document.cookie="current_pos=" + "5" + "; path=/";
		scrollFrameHeight('0');
		return false;
	});
	
	jQuery('.closeIcon').click(function(){
		jQuery('.dialog_overlay').fadeOut();
		jQuery('#modalmobtel').fadeOut();
		return false;
	});
	
	//Start of Code Change Mobile Number
	
	$("#mobile_number2").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
		return false;
		}
	});	

	$("#verif_code2").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
		return false;
		}
	});	

	jQuery('#mobile_number2').keypress(function (e) {
	 var key = e.which;
	 if(key == 13)  // the enter key code
	  {
		jQuery("#submit_mobtel2").click();
	  }
	});
	
	jQuery('#verif_code2').keypress(function (e) {
	 var key = e.which;
	 if(key == 13)  // the enter key code
	  {
		jQuery("#validate_verif_code2").click();
	  }
	});	
	
	jQuery("#submit_mobtel2").click(function(){
	
	jQuery(".error1").css("margin-top","50px");
	jQuery(".error2").css("margin-top","50px");
	
		var mobtel = jQuery("#mobile_number2").val();
		var verify = getCookie("verify");
		
	    if(document.getElementById("notglobe").checked == true)
		  var verify = "verifyNumberNonglobe";
	    else
		  var verify = "verifynumber3";
		  
		if(mobtel.length!=11) {
			jQuery("#submit_mobtel2").text("Send code");
			jQuery(".error1").show();
			jQuery(".error2").hide();
			jQuery("#verificationCode2").hide();
			jQuery("#mobile_number2").addClass("error");
			jQuery("#mobile_number2").focus();
		} else {
			jQuery("#prepaidNumber2 .errorBox").hide();
			
			jQuery.getJSON(baseurl + "prepaid/"+verify+"/" + mobtel,{ajax: 'true'}, function(j){
				if(j.status==0) {
					jQuery("#submit_mobtel2").text("Resend code");
					jQuery(".error1").hide();
					jQuery(".error2").hide(); 
					jQuery(".errorBox2").hide(); 
					jQuery("#verificationCode2").show();
					jQuery("#mobile_number2, #verif_code2").removeClass("error");
					jQuery("#verif_code2").focus();
					jQuery("#verif_code2").val('');
				} else if (j.status==-2){
				    jQuery(".error1").hide();
					jQuery(".error2").show();
					jQuery("#verificationCode").hide();
					jQuery("#mobile_number2").addClass("error");
					jQuery("#verificationCode2").hide();
					jQuery("#mobile_number2").focus();
				} else {
					jQuery(".error1").show();
					jQuery(".error2").hide();
					jQuery("#verificationCode").hide();
					jQuery("#mobile_number2").addClass("error");
					jQuery("#verificationCode2").hide();
					jQuery("#mobile_number2").focus();
				}		
			});
		}
	});
	
	jQuery("#validate_verif_code2").click(function() {
	
	jQuery(".errorBox2").css("margin-top","60px");
	
		vcode=jQuery("#verif_code2").val();
		if(vcode.length==6) {
			jQuery("#verificationCode2 .errorBox2").hide();
			
			jQuery.getJSON(baseurl + "prepaid/verifycode/" + vcode,{ajax: 'true'}, function(j){
				if(j.status==0) {
					jQuery("#verificationCode2 .errorBox2").hide();
					jQuery("#savechanges").removeClass("disable");
					jQuery("#savechanges").removeAttr("disabled", false);
					jQuery("#verif_code2").removeClass("error");
				} else {
					jQuery("#verificationCode2 .errorBox2").show();
					jQuery("#verif_code2").addClass("error");
					jQuery("#verif_code2").focus();
				}	
			});
		} else {
			jQuery("#verificationCode2 .errorBox2").show();
			jQuery("#verif_code2").addClass("error");
			jQuery("#verif_code2").focus();
		}
	});
	
	jQuery("#savechanges").click(function() {
		jsonURL = ""
		if(jQuery("#notglobe").is(":checked")) {
			noneMobtel = "None";
			jQuery("#sum-mobtel").text(noneMobtel);
			document.cookie="mobtel=" + noneMobtel + "; path=/";

			jsonURL = baseurl + "prepaid/saveNonGlobe/";
		}
		else if (!jQuery("#savechanges").hasClass("disable")) {
			document.cookie="oldmobtel=" + jQuery("#sum-mobtel").text() + "; path=/";
			var mobile_number2 = jQuery("#mobile_number2").val();
			document.cookie="mobtel=" + mobile_number2 + "; path=/";
			jQuery("#sum-mobtel").text(mobile_number2);

			jsonURL = baseurl + "prepaid/savenumber/";
		}
			
		jQuery.getJSON(jsonURL,{ajax: 'true'}, function(j){
			if(j.status==0) {
				jQuery('.dialog_overlay').fadeOut();
				jQuery('#modalmobtel').fadeOut();
			} else {
				jQuery(".error1").text(j.error);
				jQuery(".error1").show();
			}		
		});
	});
	
	jQuery("#notglobe").change(function() {
		if(jQuery(this).is(":checked")) {
			// disable buttons + input field, change saved to normal
			jQuery("#mobile_number2, #submit_mobtel2").addClass("disable").attr("disabled", "disabled");
			jQuery("#savechanges").removeClass("disable").removeAttr("disabled", false);
			jQuery("#mobile_number2").val("");
			jQuery("#mobile_number2").removeClass("error");
			jQuery(".error1").hide();
		} else {
			jQuery("#mobile_number2, #submit_mobtel2").removeClass("disable").removeAttr("disabled", false);
			jQuery("#savechanges").addClass("disable").attr("disabled", "disabled");
		}
	});
	
	//End of Code Change Mobile Number

	jQuery(".printIcon").click(function(e) {
		e.preventDefault();
		window.print();
		/*var newWin = window.frames["printf"];
		newWin.document.write('<body onload="window.print()">dddd</body>');
		newWin.document.close();*/
		
		//var body = jQuery("#orderSummary").html();
		/*var body = jQuery("html").html();
		myWindow=window.open('','','width=300,height=200,scrollbars=yes');
		myWindow.document.write(body);
		myWindow.document.close(); //missing code
		myWindow.focus();
		myWindow.print();*/

		/*var print_url = baseurl+"summary";

        jQuery("body").append('<iframe id="print_page" name="print_page" src=' + print_url + ' style="display: none; @media print { display: block; }"></iframe>');

        jQuery('#print_page').on("load", function() {
            window.frames["print_page"].focus();
            window.frames["print_page"].print();
        });*/

 		
	});
});


   function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
	var c = ca[i].replace(/\s/g, "");
	if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
   }